package com.utilities.visualiser;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

@SuppressWarnings("deprecation")
public class PrepareXMLFile {

	private Document doc = null;
	/**
	 * @param args
	 * @throws ParserConfigurationException 
	 */

	public PrepareXMLFile createFeatureSlots(String id, String strand, boolean shadow) 
	{
		Element element = (Element)doc.getElementsByTagName("cgview").item(0);
		Element featureSlot = doc.createElement("featureSlot");
		featureSlot.setAttribute("id", id);
		featureSlot.setIdAttribute("id", true);
		featureSlot.setAttribute("strand", strand);
		if (!shadow) featureSlot.setAttribute("shadow", "false");
		element.appendChild(featureSlot);
		return this;
	}

	public PrepareXMLFile createLegend(String id, String position,String font, String backgroundOpacity)
	{
		Element element = doc.getDocumentElement();
		Element legend =  doc.createElement("legend");
		legend.setAttribute("id", id);
		legend.setIdAttribute("id", true);
		legend.setAttribute("position", position);
		legend.setAttribute("backgroundOpacity", backgroundOpacity);
		legend.setAttribute("font", font);
		element.appendChild(legend);
		return this;
	}
	
	public Element createFeature(String featureSlotId, String color, String decoration, 
			String label, String hyperlink, String mouseover,
			String proportionOfThickness, String opacity, String radiusAdjustment)
	{
		Element featureSlot = doc.getElementById(featureSlotId);
		Element feature =  doc.createElement("feature");
		feature.setAttribute("color", color);
		feature.setAttribute("decoration", decoration);
		feature.setAttribute("showLabel", "true");
		if (label != null) feature.setAttribute("label", label);
		if (proportionOfThickness != null) feature.setAttribute("proportionOfThickness", proportionOfThickness);
		if (opacity != null) feature.setAttribute("opacity", opacity);
		if (radiusAdjustment != null) feature.setAttribute("radiusAdjustment", radiusAdjustment);
		if (hyperlink != null) feature.setAttribute("hyperlink", hyperlink);
		if (mouseover != null) feature.setAttribute("mouseover", mouseover);
		featureSlot.appendChild(feature);
		return feature;
	}
	
	public PrepareXMLFile createLegendItem(String legendId, String text, String font, boolean drawSwatch, String swatchColor)
	{
		Element element = doc.getElementById(legendId);
		Element legendItem =  doc.createElement("legendItem");
		legendItem.setAttribute("text", text);
		if (font != null) legendItem.setAttribute("font", font);
		if (drawSwatch)
		{
			legendItem.setAttribute("drawSwatch", "true");
			legendItem.setAttribute("swatchColor", swatchColor);
		}
		element.appendChild(legendItem);
		return this;
	}
	
	public void createFeatureRange(Element feature, String start, String stop, String proportionOfThickness)
	{
		Element range = doc.createElement("featureRange");
		range.setAttribute("start", start);
		range.setAttribute("stop", stop);
		if (proportionOfThickness != null) range.setAttribute("proportionOfThickness", proportionOfThickness);
		feature.appendChild(range);
	}
	
	public PrepareXMLFile createCGViewFromFile(String fileName) throws ParserConfigurationException, SAXException, IOException
	{
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		doc = builder.parse(fileName);
		return this;
	}

	public PrepareXMLFile createCGView(String seqLength) throws ParserConfigurationException 
	{
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = builder.newDocument();
		Element element = doc.createElement("cgview");
		element.setAttribute("id", "cgview");
		element.setIdAttribute("id", true);
		element.setAttribute("backboneRadius", "200");
		element.setAttribute("sequenceLength", seqLength);
		element.setAttribute("height", "700");
		element.setAttribute("width", "700");
		element.setAttribute("titleFont", "SansSerif, plain, 18");
		element.setAttribute("title", "Escherichia coli str. K-12,substr. MG1655");
		element.setAttribute("globalLabel", "auto");
		element.setAttribute("moveInnerLabelsToOuter", "false");
		element.setAttribute("featureThickness", "xx-large");
		element.setAttribute("tickLength", "small");
		element.setAttribute("shortTickColor", "green");
		element.setAttribute("longTickColor", "green");
		element.setAttribute("zeroTickColor", "green");
		element.setAttribute("showBorder", "false");
		doc.appendChild(element);
		return this;
	}
	
	public PrepareXMLFile initialise() throws ParserConfigurationException 
	{
		return this;
	}
	public String writeToFile(String fileName)
	{
		File fl = new File(fileName);
		try 
		{
			OutputFormat format = new OutputFormat(doc);
			format.setIndenting(true);
			XMLSerializer serializer = new XMLSerializer(
					new FileOutputStream(new File(fileName)), format);
					serializer.serialize(doc);
            return fl.getAbsolutePath();
        } 
		catch (Exception e)
		{
			e.printStackTrace();
        }
		return null;
	}
}// PrepareXMLFile
